package iotwisp.school.api.controller.pc_admin.user;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.JSONUtils;
import iotwisp.school.user.entity.LoginToken;
import iotwisp.school.user.qo.LoginTokenQO;
import iotwisp.school.user.service.LoginTokenService;

@RestController
@RequestMapping("/api/org/login_token")
public class LoginTokenController extends RestBaseController {

	@Autowired
	private LoginTokenService loginTokenService;

	@GetMapping("/query")
	public ResponseEntity<JSONResultDTO> queryRole(@RequestParam(name = "qo") String qoJson)
			throws UnsupportedEncodingException {

		LoginTokenQO qo = JSONUtils.parseObject(URLDecoder.decode(qoJson, "UTF-8"), LoginTokenQO.class);

		switch (qo.getResultType()) {
		case LoginTokenQO.RESULT_TYPE_COUNT:
			Integer count = loginTokenService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case LoginTokenQO.RESULT_TYPE_UNIQUE:
			LoginToken loginToken = loginTokenService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(loginToken));
		case LoginTokenQO.RESULT_TYPE_LIST:
			List<LoginToken> loginTokens = loginTokenService.queryList(qo);
			return ResponseEntity.ok(createSuccessResult(loginTokens));
		case LoginTokenQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = loginTokenService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}

	}
}
